package day7;

import javax.print.DocFlavor;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int[] array = new int[100];
            array[0] = 0;
            array[1] = 1;
            int  i = 2;
            for(; i < 100; i++) {
                array[i] = array[i-1] + array[i-2];
                if(n < array[i]) {
                    break;
                }
            }
            int num1 = array[i] - n;
            int num2 = n - array[i-1];
            int min = (num1 > num2 ? num2 : num1);
            System.out.println(min);
         }
    }
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int left = 0;
            int right = 1;
            while(n >= right) {
                int tmp = left + right;
                left = right;
                right = tmp;
            }
            int min = ((n - left) > (right - n) ? (right - n) : (n - left));
            System.out.println(min);
        }
    }

    public boolean chkParenthesis1(String A, int n) {
        // write code here
        if(A == null) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < n; i++) {
            if(A.charAt(i) != '(' && A.charAt(i) != ')') {
                return false;
            }
            stack.push(A.charAt(i));
            if(A.charAt(i) == ')') {
                stack.pop();
                if(stack.isEmpty() && i != n-1) {
                    return false;
                }
                stack.pop();
            }
        }
        if(stack.isEmpty()) {
            return true;
        }
        return false;
    }
    public boolean chkParenthesis2(String A, int n) {
        // write code here
        Stack<Character> stack = new Stack<>();
        for(char c : A.toCharArray()) {
            if(c == '(') {
                stack.push(c);
            }else if(c == ')') {
                if(stack.isEmpty()) {
                    return false;
                }else if (stack.peek() == '(') {
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        return stack.isEmpty();

    }

    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int fib0 = 0;
            int fib1 = 1;
            int fib = fib1 + fib0;
            while(fib < n) {
                int tmp = fib;
                fib = fib + fib1;
                fib1 = tmp;
            }
            int count = (fib-n) > (n-fib1) ? n-fib1 : fib-n;
            System.out.println(count);

        }
    }
    public static boolean chkParenthesis(String A, int n) {
        // write code here
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < n; i++) {
            if(A.charAt(i) != '(' && A.charAt(i) != ')') {
                return false;
            }
            if(A.charAt(i) == '(') {
                stack.push(A.charAt(i));
            }else if(A.charAt(i) == ')' && !stack.isEmpty() && stack.peek() == '(') {
                stack.pop();
            }

        }
        if(stack.isEmpty()) {
            return true;
        }
        return false;
    }
    public static void main(String[] args) {
        String A = "(()())";
        int n = 6;
        boolean ret = chkParenthesis(A,n);
        System.out.println(ret);
    }
}